home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TIMING.SWG / 0017_Timing Functions.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-25  |  3KB  |  136 lines

  1. {
  2.    ▌Is there an easy way to time functions and/or procedures??  I'm trying
  3.    ▌to compare a couple functions that do the samething and I would like to
  4.    ▌time them.  I've tried using the GetTime procedure but the hundredths of
  5.    ▌seconds isn't fast enough.  Can any one help?
  6.  
  7.    I think this unit may help you:
  8.  
  9. ***************************************************************************
  10. }
  11. unit tptimer;
  12.  
  13. interface
  14.  
  15. procedure cardinal(l:longint; var result:double);
  16.  
  17. procedure elapsedtime(start:longint; stop:longint; var result:double);
  18. (*Calculate time elapsed (in milliseconds) between Start and Stop*)
  19.  
  20. procedure initializetimer;
  21. (*Reprogram the timer chip to allow 1 microsecond resolution*)
  22.  
  23. procedure restoretimer;
  24. (*Restore the timer chip to its normal state*)
  25.  
  26. function readtimer:longint;
  27. (*Read the timer with 1 microsecond resolution*)
  28.  
  29. implementation
  30. uses dos;
  31.  
  32. const
  33. TimerResolution=1193181.667;
  34.  
  35. procedure cardinal(l:longint; var result:double);
  36.  Begin
  37.   if l < 0 then result:= l + 4294967296.0
  38.     else
  39.   result := l;
  40.  End;
  41.  
  42. procedure elapsedtime(start, stop:longint; var result:double);
  43.   var r:double;
  44.  Begin
  45.   cardinal(stop - start, r);
  46.   result := (1000 * r) / TimerResolution;
  47.  End;
  48.  
  49. procedure initializetimer;
  50. label NullJump1,NullJump2;
  51. Begin
  52.   port[$043]:=$034;
  53.   asm jmp NullJump1;
  54.   NullJump1:
  55.   end;
  56.   port[$040]:=$000;
  57.   asm jmp NullJump2
  58.   NullJump2:
  59.   end;
  60.   port[$040]:=$000;
  61. End;
  62.  
  63. procedure restoretimer;
  64. label NullJump1,NullJump2;
  65. Begin
  66.   port[$043]:=$036;
  67.   asm jmp NullJump1;
  68.   NullJump1:
  69.   end;
  70.   port[$040]:=$000;
  71.   asm jmp NullJump2
  72.   NullJump2:
  73.   end;
  74.   port[$040]:=$000;
  75. End;
  76.  
  77. function readtimer:longint; assembler;
  78. label done;
  79. Asm
  80.   cli             (* Disable interrupts *)
  81.   mov  dx,020h     (* Address PIC ocw3   *)
  82.   mov  al,00Ah     (* Ask to read irr    *)
  83.   out  dx,al
  84.   mov  al,00h     (* Latch timer 0 *)
  85.   out  043h,al
  86.   in   al,dx      (* Read irr      *)
  87.   mov  di,ax      (* Save it in DI *)
  88.   in   al,040h     (* Counter --> bx*)
  89.   mov  bl,al      (* LSB in BL     *)
  90.   in   al,040h
  91.   mov  bh,al      (* MSB in BH     *)
  92.   not  bx         (* Need ascending counter *)
  93.   in   al,021h     (* Read PIC imr  *)
  94.   mov  si,ax      (* Save it in SI *)
  95.   mov  al,00FFh    (* Mask all interrupts *)
  96.   out  021h,al
  97.   mov  ax,040h     (* read low word of time *)
  98.   mov  es,ax      (* from BIOS data area   *)
  99.   mov  dx,es:[06Ch]
  100.   mov  ax,si      (* Restore imr from SI   *)
  101.   out  021h,al
  102.   sti             (* Enable interrupts *)
  103.   mov  ax,di      (* Retrieve old irr  *)
  104.   test al,001h     (* Counter hit 0?    *)
  105.   jz   done       (* Jump if not       *)
  106.   cmp  bx,0FFh     (* Counter > 0x0FF?    *)
  107.   ja   done       (* Done if so        *)
  108.   inc  dx         (* Else count int req. *)
  109. done:
  110.   mov ax,bx   (* set function result *)
  111. End;
  112.  
  113. End.
  114.  
  115. ***********************************************************************
  116.  
  117. and here is a program to test the unit:
  118.  
  119. Program TestTime;
  120. uses crt, dos, tptimer;
  121.  var start_time, stop_time: longint;
  122.      time:double;
  123. Begin
  124.  Clrscr;
  125.  initializetimer;
  126.  delay(100);
  127.  start_time:=readtimer;
  128.  delay(2);
  129.  stop_time:=readtimer;
  130.  elapsedtime(start_time, stop_time, time);
  131.  writeln('elapsed time = ', time:0:10);
  132.  readln;
  133.  restoretimer;
  134. End.
  135.  
  136.